/**
 * WeeShop 声明
 * ===========================================================
 * 网站： https://www.darlang.com
 * 标题： ECShop 小程序「weeshop 」- 基于 ECShop 为后台系统开发的非官方微信商城小程序
 * 链接： https://www.darlang.com/?p=709
 * 说明： 源码已开源并遵循 Apache 2.0 协议，你有权利进行任何修改，但请保留出处，请不要删除该注释。
 * ==========================================================
 * Copyright 2019 darlang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===========================================================
 */

// index.js
import { PNT, setNavBarTitle, request, showToast, pushPagePath, formatTime, scrollLoadList } from "../../../../utils/utils";
import { GetRaply,GetDoRaply, GetSendVcode } from '../../../../utils/apis';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    recommendLst: '',
    recommendInfo: [],
    wxaCodeStatus: true,
    page: 0,
    pages: {
      page: 1,
      size: 10,
      total: 10,
      done: false,
      loading: false
    },
    flag:true,
    sms:'获取短信验证码',
    code:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    setNavBarTitle(PNT.member.apply);
    this.loginModal = this.selectComponent("#login-modal");

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.GetAccountLog();
    this.getRecommendInfo();
  },

  /**
   * 登录回调
   * @author darlang
   */
  loginCallback(cb) {
    if (cb.detail.type === 'success') {
      // this.getRecommendList();
      this.getRecommendInfo();
    }
  },



  /**
   * 获取推荐信息
   * @author darlang
   */
  getRecommendInfo() {
    if (!this.loginModal.check()) {
      return false;
    }
    wx.showLoading({ title: '加载中...', mask: true });
    var user = wx.getStorageSync('userInfo');
    if (!user || user == undefined) {
      return false;
    }
    var page = this.data.page;
    GetRaply(user.user_id).then(res => {
      res.data.bank_card = res.data.bank_card.substr(0, 4) + "******" + res.data.bank_card.substr(-4)
      console.log(res)
      var recommendInfo = this.data.recommendInfo;
      if (res.error_code ==  10000) {
        this.setData({
          recommendInfo: res.data,
        })
      } else {
        wx.showLoading({
          title: '请求失败',
        })
        setTimeout(function (e) {
          wx.hideLoading();
        }, 2000)
      }
      var info = wx.getStorageSync('userInfo');
      this.setData({
        info: info
      })
    });
  },

  formSubmit(e){
    var that = this;
    if(!that.data.flag){
      wx.showLoading({
        title: '提交中...',
      })
      setTimeout(function(){
        wx.hideLoading();
      },2000)
      that.setData({
        flag:false,
      })
    }
    if(!e.detail.value.amount){
      wx.showToast({
        title: '请输入提现金额',
        icon:'none'
      })
      that.setData({
        flag: true,
      })
      return;
    }
    if (!e.detail.value.vcode) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      that.setData({
        flag: true,
      })
      return;
    }
    var data = {};
    data.user_id = that.data.info.user_id;
    data.vcode = e.detail.value.vcode;
    data.amount = e.detail.value.amount;
    data.type = 'vcode';
    GetDoRaply(data).then(res=>{
      if(res.error_code == 10000){
        showToast('提交成功')
        setTimeout(()=>{
          wx.navigateBack({
            delta:1,
          },2000)
        })

      }else{
        wx.showToast({
          title: '提交失败',
        })
      }
    })


  },

  //获取验证码

  sms:function(e){
    var that = this;
    if(!this.data.code){
      var sms = that.data.sms;
      wx.showToast({
        title: sms,
        icon:'none'
      })
      return;
    }
    that.setData({
      code:false
    })
    var type = 'vcode';
    var phone = this.data.info.mobile_phone;
    // phone = '17310606312';
    var user_id = this.data.info.user_id;
    var data = {type,phone,user_id};
    GetSendVcode(data).then(res=>{
      if(res.error_code == 10000){
        wx.showToast({
          title: '验证码发送成功',
        })
        that.time()
      }
    }).catch(function(reason){
      wx.showToast({
        title: '服务器忙',
      })
      that.onLoad();
    })
  },
  time:function(e){
    var that = this;
    var time = 120;
    var inter = setInterval(function(){
      time--;
      that.setData({
        sms:"请"+time+"秒后再发"
      })
      if(time == 0){
        that.setData({
          sms: '获取短信验证码',
          code:true,
        })
        clearInterval(inter)
      }
    },1000)
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: "#F6344D",
      animation: {
        duration: 300,
        timingFunc: "linear"
      }
    });
    // this.getWxQrcode();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getRecommendInfo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const u = wx.getStorageSync('userInfo') || '';
    if (!u) {
      showToast('登录异常,分享失败');
      return false;
    }
    var shareName = this.data.siteInfo.name || '';
    var imgUrl = '/images/default_activity-' + parseInt(Math.random() * 2) + '.png';
    var shareLink = '/pages/index/index?inviteCode=' + (u.id || '');
    return {
      title: shareName || PNT.default,
      imageUrl: imgUrl || '/images/default_image.png',
      path: shareLink,
      success() { },
      fail() { },
      complete() { }
    };
  }
});